/*
 *	Desktop Layers
 * 	Responsible for managing layers and items belongs to them.
 */

jt.module('DesktopLayers', function(context) {
	
	// useful shorthands
	var canvas = context.canvas,
		settings = context.settings,
		gh = context.settings.grid.h,
		gv = context.settings.grid.v,
		fn = context.fn;
		
	var layers = {},
	activeLayer = 'default';
	layers['default'] = [];
	
	// Module functionality
	var hideLayer = function(layerName) {	
		if(!layers[layerName]) {
			return;
		}

		for(var i = 0, len =  layers[layerName].length; i < len; i++) {
			context.event('hideItem', layers[layerName][i]);
		}
	},
	
	showLayer = function(layerName) {	
		if(!layers[layerName]) {
			return;
		}
		
		for(var i = 0, len =  layers[layerName].length; i < len; i++) {
			context.event('showItem', layers[layerName][i]);
		}
	},
	
	changeLayer = function(layerName) {
		
		hideLayer(activeLayer);
		showLayer(layerName);
		activeLayer = layerName;
	};
	
	/* 	 
	 * Desktop fn
	 */
	
	fn.extend({
		changeLayer: changeLayer,
		showLayer: showLayer,
		hideLayer: hideLayer
	});
	
	return {
		
		events: {
			/*
			 *	Summary: Initialize module event handler
			 */
			init: function() {
				context.log('[DesktopLayers] init');
			},
			
			addItem: function(item, settings) {
				
				var layerName = settings.layer || 'default';
		
				if (!layers[layerName]) {
					layers[layerName] = [];
				}
				
				layers[layerName].push(item.id);
				item.layer = layerName;
			},
			
			removeItem: function(itemId) {
				layers[context.getItemById(itemId).layer].remove(itemId);
			},
		}
	}
	
});